package objectFactory.javax.sql.DataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import vo.UserVO;

public class UserDao {
		private DataSource dataSource;

		public void setDataSource(DataSource dataSource) {
			this.dataSource = dataSource;
		}
		
		public void add(UserVO user) throws ClassNotFoundException,SQLException{
			Connection c = dataSource.getConnection();
			PreparedStatement psmt = c.prepareStatement("insert into users(id,name,password) values (?,?,?)");
			psmt.setString(1, user.getId());
			psmt.setString(2,user.getName());
			psmt.setString(3,user.getPassword());
			
			psmt.executeUpdate();
			
			psmt.close();
			c.close();
	}

	public UserVO get(String id) throws ClassNotFoundException,SQLException{
		UserVO user = new UserVO();
		
		Connection c = dataSource.getConnection();
		PreparedStatement psmt = c.prepareStatement("select * from users where id = ?");
		
		psmt.setString(1, id);
		
		ResultSet rs =  psmt.executeQuery();
		
		while(rs.next()){
			user.setId(rs.getString("id"));
			user.setName(rs.getString("name"));
			user.setPassword(rs.getString("password"));
		}
		
		
		rs.close();
		psmt.close();
		c.close();
		
		return user;	
		
}

}
